<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_COOKIE
	Public Domain
	Public Expires
	Public Path
	Public Secure
	Public Unit
	
	Private Sub Class_Initialize
		call init
	End Sub
	
	'初始化
	Public Sub init
		Domain = C_("COOKIE_DOMAIN")
		Expires = C_("COOKIE_EXPIRES")
		Path = C_("COOKIE_PATH")
		Secure = CBool(C_("COOKIE_SECURE"))
		Unit = C_("COOKIE_EXPIRES_UNIT")
	end Sub
	
	Public Function Count()
		dim x,y
		Count = 0
		 for each x in Request.Cookies
		  if Request.Cookies(x).HasKeys then
			for each y in Request.Cookies(x)
			  Count = Count + 1
			next
		  else
			Count = Count + 1
		  end if
		next
	End Function
	
	'移除一个键
	Public Sub Remove( byval key )
		'on error resume next
		if isArray( key ) then
			key = key(0)
		end if
		if Exists(key) Then		
			Response.Cookies( key ).Expires = DateDiff( "d",1,now() )			
		end if
	end Sub
	
	'给cookie中设置参数
	' cookie.get(key)					Request.Cookies(key)
	' cookie.get(array(key))			Request.Cookies(key)
	' cookie.get(array(key1,key2))		Request.Cookies(key1)(key2)
	Public Function [Get]( byval key )
		if isArray( key ) then
			if ubound(key) = 0 then
				[Get] = Request.Cookies(key(0))
			else
				[Get] = Request.Cookies(key(0))(key(1))
			end if
		else
			[Get] = Request.Cookies(key)
		end if
	End Function
	
	
	' 判断某个键值是否存在于cookie中
	' cookie.Exists(key)					判断Request.Cookies(key)是否存在
	' cookie.set array(key),value			Response.Cookies(key) = value 
	' cookie.set array(key1,key2),value		Response.Cookies(key1)(key2) = value 
	Public Function Exists( byval key )
		dim x,y
		Exists = false
		if isArray( key ) then
			if ubound(key) = 0 then
				key(0) = UCase( key(0) )
				for each x in Request.Cookies
					if UCase(x) = key(0) then
						Exists = True
						Exit Function
					end if
				next
			else
				key(0) = UCase( key(0) )
				key(1) = UCase( key(1) )
				for each x in Request.Cookies
					if Request.Cookies(x).HasKeys then
						if UCase(x) = key(0) then
							for each y in Request.Cookies(x)
								if UCase(y) = key(1) then
									Exists = True
									exit Function
								end if
							next
						end if
					end if
				next
			end if
		else
			key = UCase( key )
			for each x in Request.Cookies
				if UCase(x) = key then
					Exists = True
					Exit Function
				end if
			next
		end if
	End Function	

	
	'给cookie中设置参数
	' cookie.set key,value					Response.Cookies(key) = value 
	' cookie.set array(key),value			Response.Cookies(key) = value 
	' cookie.set array(key1,key2),value		Response.Cookies(key1)(key2) = value 
	Public sub [Set]( byval key, byval val )
		'on error resume next
		if isArray( key ) then
			if ubound(key) = 0 then
				Response.Cookies( key(0) ) = val
				with Response.Cookies( key(0) )
					.Domain = Domain
					.Expires = DateAdd(Unit,Expires,now()) 
					.Secure = Secure
				end with
			else
				Response.Cookies( key(0) )( key(1) ) = val
				with Response.Cookies( key(0) )
					.Domain = Domain
					.Expires = DateAdd(Unit,Expires,now() )
					.Secure = Secure
				end with
			end if
		else
			Response.Cookies( key ) = val
			with Response.Cookies( key )
				.Domain = Domain
				.Expires = DateAdd(Unit,Expires,now() )
				.Secure = Secure			
			end with
		end if
	End sub
End Class
%>